/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaCredentialMapping;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;
import han.bifang.common.utils.CommonUtils;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class CredentialMappingDao {
	
	public static String createCredentialMapping(SoaCredentialMapping c)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaCredentialMapping s where s.localUser = ? and "
				+ "s.remoteDomainId = ? ";
			List ret = null;
			if(c.getRemoteServiceCode() != null ){
				hql += " and s.remoteServiceCode = ?";
				ret = DaoManager.getDao().find(hql, new Object[]{c.getLocalUser(),c.getRemoteDomainId(),
						c.getRemoteServiceCode()});
			}else{
				ret = DaoManager.getDao().find(hql, new Object[]{c.getLocalUser(),c.getRemoteDomainId()});
			}
			
			if(ret.size() > 0){
				throw new BifangException(BifangErrorCode.CREDENTIAL_MAPPING_EXIST_ERROR,"this credential mapping exists");
			}
			DaoManager.getDao().save(c);
			DaoManager.getDao().flush();
			tx.commit();

			return c.getId();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserDao.class);
			logger.error("CredentialMappingDao.createCredentialMapping()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createCredentialMapping() error", e);
		} finally {
			tx.end();
		}

		
	}

	
	public static SoaCredentialMapping getCredentialMapping(String localUser,
			String remoteDomainId, String remoteServiceCode)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaCredentialMapping s where s.localUser = ? and "
				+ "s.remoteDomainId = ? ";
			List ret = null;
			if(! CommonUtils.isEmpty(remoteServiceCode) ){
				hql += " and s.remoteServiceCode = ?";
				ret = DaoManager.getDao().find(hql, new Object[]{localUser,remoteDomainId,
						remoteServiceCode});
			}else{
				ret = DaoManager.getDao().find(hql, new Object[]{localUser,remoteDomainId});
			}
			tx.commit();
			if(ret.size() > 0){
				return (SoaCredentialMapping)ret.get(0);
			}
			
			
			

			return null;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserDao.class);
			logger.error("CredentialMappingDao.getCredentialMapping()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getCredentialMapping() error", e);
		} finally {
			tx.end();
		}
	}

	
	public static SoaCredentialMapping getCredentialMappingById(String id)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			
			Object o = DaoManager.getDao().get(SoaCredentialMapping.class, id);
			
			tx.commit();
			return (SoaCredentialMapping)o;
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserDao.class);
			logger.error("CredentialMappingDao.getCredentialMappingById()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getCredentialMappingById() error", e);
		} finally {
			tx.end();
		}



	}

	
	public static void modifyCredentialMapping(SoaCredentialMapping c)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaCredentialMapping s where s.localUser = ? and "
				+ "s.remoteDomainId = ? ";
			List ret = null;
			if(c.getRemoteServiceCode() != null ){
				hql += " and s.remoteServiceCode = ?";
				ret = DaoManager.getDao().find(hql, new Object[]{c.getLocalUser(),c.getRemoteDomainId(),
						c.getRemoteServiceCode()});
			}else{
				ret = DaoManager.getDao().find(hql, new Object[]{c.getLocalUser(),c.getRemoteDomainId()});
			}
			
			if(ret.size() > 0){
				throw new BifangException(BifangErrorCode.CREDENTIAL_MAPPING_EXIST_ERROR,"this credential mapping exists");
			}
			DaoManager.getDao().merge(c);
			DaoManager.getDao().flush();
			tx.commit();

			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserDao.class);
			logger.error("CredentialMappingDao.modifyCredentialMapping()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyCredentialMapping() error", e);
		} finally {
			tx.end();
		}


	}

	
	public static void removeCredentialMapping(String id) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			
			Object o = DaoManager.getDao().get(SoaCredentialMapping.class, id);
			DaoManager.getDao().delete(o);
			DaoManager.getDao().flush();
			tx.commit();

			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(UserDao.class);
			logger.error("CredentialMappingDao.removeCredentialMapping()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeCredentialMapping() error", e);
		} finally {
			tx.end();
		}



	}
}
